package com.xinlianshiye.im.utils;

import com.xinlianshiye.im.bean.User;
import java.util.*;

public class SortUtils {
    static final Character[] str= new Character[] { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z','#'};

    public  static Map<String,List<User>> sort(List<User> list){
        Map<String,List<User>> map= new LinkedHashMap<>();
        for (int i = 0; i <str.length ; i++) {
            map.put(String.valueOf(str[i]),new ArrayList<>());
        }
        for (int i = 0; i <list.size() ; i++) {
            String  name = StringControverPinYin.getPinYinHeadChar(list.get(i).getUsername());
            String charStr=name.substring(0,1).toUpperCase();
            Boolean bool=false;
            for (char c : str) {
                if(charStr.equalsIgnoreCase(String.valueOf(c))){
                    bool=true;
                    break;
                }
            }
                if(bool){
                    map.get(charStr).add(list.get(i));
                }else{
                    map.get(String.valueOf(str[str.length-1])).add(list.get(i));
                }
        }
        for (List<User> users:map.values()){
            users.sort(
                    ((o1, o2) -> {
                        Integer num=0;
                        String  name1 = StringControverPinYin.getPinYinHeadChar(o1.getUsername());
                        String  name2 = StringControverPinYin.getPinYinHeadChar(o2.getUsername());
                        int code;
                        do {
                            String chat1=name1.substring(num,num+1).toUpperCase();
                            String chat2=name2.substring(num,num+1).toUpperCase();
                            if(chat1.hashCode()==chat2.hashCode()){
                                num++;
                                continue;
                            }else{
                                code=   chat1.hashCode()-chat2.hashCode();
                                break;
                            }
                        }while (true);
                            return code;
                    })
            );
        }

        return  map;
    }

    public static void main(String[] args) {
        List<User> listu = setUser();
        Map<String,List<User>> map=sort(listu);
    }

    private static List<User> setUser() {
        User user= new User();
        user.setUsername("z张人");
        user.setId(1);
        User user1= new User();
        user1.setUsername("z李人");
        user1.setId(2);
        User user2= new User();
        user2.setUsername("z啊人");
        user2.setId(3);
        User user3= new User();
        user3.setUsername("Z不人");
        user3.setId(3);
        User user4= new User();
        user4.setUsername("正常dg人");
        user4.setId(4);
        User user5= new User();
        user5.setUsername("字段ggdg人");
        user5.setId(5);
        User user6= new User();
        user6.setUsername("则额ggdg人");
        user6.setId(6);
        User user7= new User();
        user7.setUsername("转发爱ggdg人");
        user7.setId(7);
        User user8= new User();
        user8.setUsername("wq爱ggdg人");
        user8.setId(8);
        User user9= new User();
        user9.setUsername("vs爱ggdg人");
        user9.setId(9);
        User user10= new User();
        user10.setUsername("123424231412");
        user10.setId(10);
        List<User> listu=new ArrayList<>();
        listu.add(user);
        listu.add(user1);
        listu.add(user2);
        listu.add(user3);
        listu.add(user4);
        listu.add(user5);
        listu.add(user6);
        listu.add(user7);
        listu.add(user8);
        listu.add(user9);
        listu.add(user10);
        return listu;
    }
}
